﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XingLucifer.IBase.Enums;

namespace UIWindows.Tools.Helper
{
    public class GetTypeByStringHelper
    {
        public static Type GetTypeByString(string type)
        {
            try
            {
                switch (type.ToLower())
                {
                    case "string":
                        return Type.GetType("System.String", true, true);
                    case "int":
                        return Type.GetType("System.Int32", true, true);
                    case "double":
                        return Type.GetType("System.Double", true, true);
                    case "float":
                        return Type.GetType("System.Single", true, true);
                    case "decimal":
                        return Type.GetType("System.Decimal", true, true);
                    case "long":
                        return Type.GetType("System.Int64", true, true);
                    case "uint":
                        return Type.GetType("System.UInt32", true, true);
                    case "ulong":
                        return Type.GetType("System.UInt64", true, true);
                    case "date":
                    case "datetime":
                        return Type.GetType("System.DateTime", true, true);
                    case "short":
                        return Type.GetType("System.Int16", true, true);
                    case "ushort":
                        return Type.GetType("System.UInt16", true, true);
                    case "bool":
                        return Type.GetType("System.Boolean", true, true);
                    case "byte":
                        return Type.GetType("System.Byte", true, true);
                    case "sbyte":
                        return Type.GetType("System.SByte", true, true);
                    case "char":
                        return Type.GetType("System.Char", true, true);
                    case "object":
                        return Type.GetType("System.Object", true, true);
                    case "guid":
                        return Type.GetType("System.Guid", true, true);
                    default:
                        return Type.GetType(type, true, true);
                }
            }
            catch (Exception)
            {
                return Type.GetType("System.String", true, true);
            }

        }

        public static object TryTypeConvert(Type t, object obj, string propetryName)
        {
            try
            {
                if (obj == null)
                {
                    DialogLogHelper.RunLog($"空值转换：属性：{propetryName} ;值为空！", MessageLevelType.错误);
                    return Activator.CreateInstance(t);
                }
                else
                {
                    if (t.Name == "DateTime")
                    {
                        if (long.TryParse(obj.ToString(), out long l))
                        {
                            return UIWindows.Tools.Helper.ConvertHelper.GetDateTime(l);
                        }
                        else
                        {
                            DialogLogHelper.RunLog($"时间型无法转换：属性：{propetryName} ;值{obj}", MessageLevelType.错误);
                            return 0;
                        }

                    }
                    else
                        return Convert.ChangeType(obj, t);
                }

            }
            catch (Exception)
            {
                DialogLogHelper.RunLog($"转换值异常：属性：{propetryName} ;值: {obj.ToString()}", MessageLevelType.错误);
            }
            return Activator.CreateInstance(t); 
        }
    }
}
